﻿using GalaSoft.MvvmLight.Messaging;
using HRM.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class StaffService:IEntityService<NhanVien>
    {
        public List<NhanVien> GetAll()
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var list = entities.NhanViens.Include("TrinhDoHocVan").Include("NguoiSuDung").ToList();
                return list;
            }
        }

        public bool Add(NhanVien entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                entities.NhanViens.Add(entity);
                entities.SaveChanges();
                return true;
            
            }
        }

        public bool Update(NhanVien entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.NhanViens.Where(x => x.MaNhanVien == entity.MaNhanVien).FirstOrDefault();
                if (query != null)
                {
                    try
                    {
                        query.HoTen = entity.HoTen;
                        query.GioiTinh = entity.GioiTinh;
                        query.NgaySinh = entity.NgaySinh;
                        query.CMND = entity.CMND;
                        query.NgayCapCMND = entity.NgayCapCMND;
                        query.NoiCapCMND = entity.NoiCapCMND;
                        query.SDT = entity.SDT;
                        query.DiaChi = entity.DiaChi;
                        query.DanToc = entity.DanToc;
                        query.Email = entity.Email;
                        query.QueQuan = entity.QueQuan;
                        query.MaSoThue = entity.MaSoThue;
                        query.MaTDHV = entity.MaTDHV;
                        query.AnhDaiDien = entity.AnhDaiDien;
                        entities.SaveChanges();
                        Messenger.Default.Send(new StatusMessage("Đã thêm nhân viên", 3000, StatusType.Done));
                        return true;
                    }
                    catch(Exception ex)
                    {
                        return false;
                    }
                }
                return false;
            
            }
        }

        public bool Delete(NhanVien entity)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.NhanViens.Where(x => x.MaNhanVien == entity.MaNhanVien).FirstOrDefault();
                if (query != null)
                {
                    try
                    {
                        entities.NhanViens.Remove(query);
                        entities.SaveChanges();
                        return true;
                    }
                    catch (Exception ex)
                    {
                        Messenger.Default.Send(StatusMessage.KeyDependentMessage());
                    }
                }
                return false;
            }
        }

        public bool DeleteList(List<NhanVien> list)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                foreach (NhanVien nv in list)
                {
                    var query = entities.NhanViens.Where(x => x.MaNhanVien == nv.MaNhanVien).FirstOrDefault();
                    if (query != null)
                    {
                        entities.NhanViens.Remove(query);
                    }
                }
                try
                {
                    entities.SaveChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    Messenger.Default.Send(StatusMessage.KeyDependentMessage());
                }
                return false;
                
            }
        }

        public List<NhanVien> GetPaging(int pageIndex, int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                //.Include("NguoiSuDung").Include("KinhNghiems")
                //    .Include("QuaTrinhCongTacs").Include("ThamGiaDuAns").Include("ThoiViecs").Include("BangLuongs").Include("ChungChis")
                return entities.NhanViens.Include("TrinhDoHocVan")
                    .OrderBy(p => p.MaNhanVien).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            }
        }

        public int GetNumberOfPages(int pageSize)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                int count = entities.NhanViens.Count();
                int pages = count / pageSize;
                if (count > pages * pageSize) return pages + 1;
                return pages;
            }
        }
    }
}
